package cn.pku.ss.sirx.sna.ib.bean;

import java.util.Date;


/**
 * Abstract of a reply tree, constructed by tree nodes.
 * Every tree node represents a message, and all tree nodes belonging to the same
 * reply tree have similar time values (not exactly same, because someone may reply at another time).
 * In a reply tree, times increases from top to bottom, and from left to right.
 *
 * @author SirX
 * @version 1.0
 */
public class ReplyTree {
    
    /**
     * Constructor.
     * @param root root node of the reply tree
     */
    public ReplyTree(ReplyTreeNode root) {
        this.root = root;
    }
    
    public Date getDate() {
        return this.root.getMessage().getTime();
    }
    
    public String getReplierId() {
        return this.root.getMessage().getFromId();
    }

    public ReplyTreeNode getRoot() {
        return root;
    }
    
    public ReplyTree getPrevious() {
        return previous;
    }
    
    public void setPrevious(ReplyTree previous) {
        this.previous = previous;
    }
    
    public ReplyTree getNext() {
        return next;
    }
    
    public void setNext(ReplyTree next) {
        this.next = next;
    }
    
    @Override
    public String toString() {
        return this.toString(this.root);
    }
    
    //////////////////////////////////////////////////////////////////////////////

    // preorder travel
    private String toString(ReplyTreeNode node) {
        StringBuffer result = new StringBuffer(node.getMessage().getFromId());
        for (ReplyTreeNode child : node.getChildren()) {
            result.append(toString(child));
        }
        return result.toString();
        
    }
    
    //////////////////////////////////////////////////////////////////////////////

    private ReplyTreeNode root;
    private ReplyTree previous;
    private ReplyTree next;

}
